package com.jpcc.vw.onlineRadio.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.bean.NewSearchAlbumResultBean;
import com.jpcc.vw.onlineRadio.model.bean.SimpleAlbumRadioEntity;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.utils.ViewHolderUtils;
import com.jpcc.vw.onlineRadio.widget.Indicator;

import java.util.ArrayList;
import java.util.List;

public class SearchAlbumsAdapter extends BaseAdapter {

    private List<SimpleAlbumRadioEntity> results = new ArrayList<>();
    private Context cx;

    public SearchAlbumsAdapter(List<SimpleAlbumRadioEntity> datas, Context context) {
        if (datas != null) {
            results.addAll(datas);
            cx = context;
        }
    }


    @Override
    public int getCount() {
        return results.size();
    }

    @Override
    public Object getItem(int position) {
        return results.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_search_result, parent, false);
        }
        initViews(convertView, position);
        return convertView;
    }


    private void initViews(View convertView, int position) {
        SimpleAlbumRadioEntity entity = results.get(position);
        TextView tv_item_count = ViewHolderUtils.get(convertView, R.id.tv_item_count);
        TextView tv_title = ViewHolderUtils.get(convertView, R.id.tv_title);
        ImageView imv_cover=ViewHolderUtils.get(convertView,R.id.imv_cover);
//        Indicator imv_playing=ViewHolderUtils.get(convertView,R.id.imv_playing);
        ImageView imv_is_vip_mark=ViewHolderUtils.get(convertView,R.id.imv_is_vip_mark);
        RelativeLayout layout_playing=ViewHolderUtils.get(convertView,R.id.layout_playing);
        TextView tv_host_name=ViewHolderUtils.get(convertView,R.id.tv_host_name);
        ImageView imv_play_status=ViewHolderUtils.get(convertView,R.id.imv_play_status);
        tv_item_count.setText(position + "");
        tv_item_count.setVisibility(View.INVISIBLE);
        tv_title.setText(entity.getTitle());
        Glide.with(cx).load(entity.getCover()).into(imv_cover);
        if(AlbumRadioAudioPlayer.get().getPlayMusic()!=null&&AlbumRadioAudioPlayer.get().getPlayMusic().getAlbumId()==entity.getAlbumId()&&"radio".equals(entity.getType())){
//            imv_playing.setVisibility(View.VISIBLE);
            imv_play_status.setVisibility(View.VISIBLE);
        }
        else{
//            imv_playing.setVisibility(View.GONE);
            imv_play_status.setVisibility(View.GONE);
        }
        tv_host_name.setText(entity.getHostName());
        String bgColor = "#404040";
        if ((AlbumRadioAudioPlayer.get().getPlayMusic() != null) && "radio".equals(AlbumRadioAudioPlayer.get().getPlayMusic().getType())) {
            if (AlbumRadioAudioPlayer.get().getPlayMusic().getAlbumId() == entity.getAlbumId()) {
//                imv_playing.setVisibility(View.VISIBLE);
                imv_play_status.setVisibility(View.VISIBLE);
                if (Constants.isRelease) {
                    try {
                        boolean atmosphereStatus = AtmosphereLampManager.getInstance(cx).isHmiAtmosphereLampOn();
                        String color = AtmosphereLampManager.getInstance(cx).getHmiAtmosphereLampColor();
                        if (atmosphereStatus) {
                            if (!TextUtils.isEmpty(color)) {
                                bgColor = color;
                            }
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
//                layout_playing.setBackgroundColor(Color.parseColor(bgColor));
                layout_playing.setBackgroundColor(Color.parseColor("#000000"));
            }
            else{
//                imv_playing.setVisibility(View.GONE);
                imv_play_status.setVisibility(View.GONE);
                layout_playing.setBackgroundColor(Color.parseColor("#000000"));
            }


            if(AlbumRadioAudioPlayer.get().isPlaying()||AlbumRadioAudioPlayer.get().isPreparing()){
//                imv_playing.setIndicatorPause(true);
                imv_play_status.setImageResource(R.drawable.ic_play_in_list);
            }
            else{
//                imv_playing.setIndicatorPause(false);
                imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
            }



        } else {
//            imv_playing.setVisibility(View.GONE);
            imv_play_status.setVisibility(View.GONE);
            layout_playing.setBackgroundColor(Color.parseColor("#000000"));
        }


        if("album".equals(entity.getType())){
            if(entity.isVipFree()){
                imv_is_vip_mark.setVisibility(View.VISIBLE);
            }
            else{
                imv_is_vip_mark.setVisibility(View.GONE);
            }
        }
        else{
            imv_is_vip_mark.setVisibility(View.GONE);
        }

    }


    public void setDatas(List<SimpleAlbumRadioEntity> datas) {
        if (datas != null) {
            results.clear();
            results.addAll(datas);
        }
    }
}
